const { imgUrl } = getApp().globalData;
import { publicApi } from "@/utils/request/api/public";
import { mineApi } from "@/utils/request/api/mine";
import Toast from "@vant/weapp/toast/toast";
Page({
  data: {
    imgUrl: imgUrl + "mine/",
    status: 0, // 0 未认证 1 认证中 2 认证不通过  3 认证通过
    company: {
      name: "",
      license_file_url: "", //营业执照
      license_no: "",
      legal_person: "", //法人
      registered_funds: "", //注册资金
      business_term: "", //营业期限
      registration_authority: "", //登记机构
      industry: "", //行业
      business_scope: "", //经营
      description: "", //简介
      idcard_front_side_url: "", //身份证正面照
      idcard_back_side_url: "", //身份证背面照
      province: "广东省", //省份
      city: "广州市", //城市
      district: "天河区", //区县
      address: "xx街道xx小区", //详细地址
    },
    registries: ["机构1", "机构2", "机构3", "机构4"],
    industryList: [],
    operates: ["经营1", "经营2", "经营3", "经营4"],
  },
  change(event) {
    const {
      currentTarget: {
        dataset: { type },
      },
    } = event;
    let company = JSON.parse(JSON.stringify(this.data.company));
    company[type] = event.detail.value;
    this.setData({
      company,
    });
  },
  select(event) {
    const {
      currentTarget: {
        dataset: { type, origin },
      },
    } = event;
    let company = JSON.parse(JSON.stringify(this.data.company));
    company[type] = this.data[origin][event.detail.value / 1];
    this.setData({
      company,
    });
  },
  chooseImage(event) {
    const that = this;
    const {
      currentTarget: {
        dataset: { sfz },
      },
    } = event;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (res) {
        const filePath = res.tempFilePaths[0];
        const formData = {
          fileVal: filePath,
        };

        publicApi
          .uploadFile({
            filePath,
            formData,
          })
          .then((res) => {
            let obj = JSON.parse(JSON.stringify(that.data.company));
            if (sfz == 0) {
              obj.idcard_front_side_url = res.data;
            } else if (sfz == 1) {
              obj.idcard_back_side_url = res.data;
            } else if (sfz == 2) {
              obj.license_file_url = res.data;
            }
            that.setData({
              company: obj,
            });
          });
      },
    });
  },
  savePreAdd() {
    wx.setStorageSync(
      "pre_add_company_info",
      JSON.stringify(this.data.company)
    );
    Toast.success("保存成功！");
  },
  toAuth() {
    if (!this.data.company.name.trim()) {
      Toast.fail("请输入公司名称！");
      return;
    }
    if (!this.data.company.license_no.trim()) {
      Toast.fail("请输入营业执照注册号！");
      return;
    }
    if (!this.data.company.legal_person.trim()) {
      Toast.fail("请输入法人！");
      return;
    }
    if (!this.data.company.registered_funds.trim()) {
      Toast.fail("请输入注册资金！");
      return;
    }
    if (!this.data.company.business_term.trim()) {
      Toast.fail("请输入营业期限！");
      return;
    }
    if (!this.data.company.registration_authority.trim()) {
      Toast.fail("请输入登记机构！");
      return;
    }
    if (!this.data.company.industry.trim()) {
      Toast.fail("请输入行业！");
      return;
    }
    if (!this.data.company.business_scope.trim()) {
      Toast.fail("请输入经营！");
      return;
    }
    if (!this.data.company.idcard_front_side_url.trim()) {
      Toast.fail("请上传身份证正面照！");
      return;
    }
    if (!this.data.company.idcard_back_side_url.trim()) {
      Toast.fail("请上传身份证背面照！");
      return;
    }
    if (!this.data.company.license_file_url.trim()) {
      Toast.fail("请上传营业执照！");
      return;
    }
    mineApi
      .addCompanyInfo(this.data.company)
      .then((res) => {
        if (res.err_code == 0) {
          Toast.success(res.err_msg);
          wx.navigateBack();
        } else {
          Toast.fail(res.err_msg);
        }
      })
      .catch((err) => {
        Toast.fail("认证失败，请稍后再试！");
      });
  },
  onLoad(options) {
    if (wx.getStorageSync("pre_add_company_info")) {
      this.setData({
        company: JSON.parse(wx.getStorageSync("pre_add_company_info")),
      });
    } else {
      wx.setStorageSync(
        "pre_add_company_info",
        JSON.stringify(this.data.company)
      );
    }

    publicApi
      .getIndustryList()
      .then((res) => {
        this.setData({
          industryList: res.data.map((item) => item.name),
        });
      })
      .catch((err) => {
        Toast.fail("获取信息失败，请稍后再试！");
      });
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
